package cn.wp.ewallet.upms.web.controller;

import cn.wp.common.core.Wp;
import cn.wp.common.core.authentication.AuthenticationInfo;
import cn.wp.common.core.authentication.AuthenticationProperties;
import cn.wp.common.core.dto.WpResult;
import cn.wp.common.core.exception.WpAuthenticationException;
import cn.wp.common.core.util.CookieUtil;
import cn.wp.common.core.web.WpWebContext;
import cn.wp.common.core.web.annotation.ClearInterceptor;
import cn.wp.common.core.web.interceptor.LoginInterceptor;
import cn.wp.ewallet.upms.service.WalletLoginService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 用户登录、注销
 * Created by chenyb on 2019/09/23
 */
@RestController
@Api(value="用户controller",tags={"用户登录注销接口"})
@RequestMapping(Wp.WebContext + "/ewallet/upms/login")
public class LoginController {

    @Autowired
    @Qualifier("appLoginService")
    private WalletLoginService appLoginService;

    @Autowired
    @Qualifier("adminLoginService")
    private WalletLoginService adminLoginService;

    @Autowired
    private AuthenticationProperties authenticationProperties;


    /**
     * -----------------------admin---------------------------
     *
     */

    /**
     * 管理端登录
     * @return
     */
    @RequestMapping("/loginAdmin")
    @ClearInterceptor(LoginInterceptor.class)
    public WpResult loginAdmin(String username,String password){
        try{
            AuthenticationInfo authenticationInfo = adminLoginService.login(username,password);
            String tokenName = authenticationProperties.getTokenName();
            // 写入cookie , 非前后端分离时可用
            CookieUtil.addCookie(WpWebContext.getResponse(),tokenName,authenticationInfo.getToken(),-1);
            return new WpResult().success(authenticationInfo);
        }catch (WpAuthenticationException e){
            return new WpResult().failure(e.getCode() + "",e.getMessage());
        }
    }


    /**
     * 修改登录密码
     * @return
     */
    @PostMapping("/passwordAdmin")
    @ApiOperation("修改登录密码")
    public WpResult passwordAdmin(String oldPassword,String newPassword){
        return adminLoginService.modifyPassword(oldPassword,newPassword);
    }

    /**
     * 退出登录
     * @return
     */
    @RequestMapping("/logoutAdmin")
    public WpResult logoutAdmin(){
        String tokenName = authenticationProperties.getTokenName();
        adminLoginService.logout();
        // 移除cookie
        CookieUtil.addCookie(WpWebContext.getResponse(),tokenName,null,0);
        return new WpResult<AuthenticationInfo>().success();
    }


    /**
     * -----------------------App---------------------------
     *
     */

    /**
     * App登录
     * @return
     */
    @RequestMapping("/loginApp")
    @ClearInterceptor(LoginInterceptor.class)
    public WpResult loginApp(String username){
        try{
            // 手机号登录时不需要密码，匹配默认的登录模式统一处理，设置默认密码，并无具体作用
            AuthenticationInfo authenticationInfo = appLoginService.login(username,"111");
            String tokenName = authenticationProperties.getTokenName();
            // 写入cookie , 非前后端分离时可用
            CookieUtil.addCookie(WpWebContext.getResponse(),tokenName,authenticationInfo.getToken(),-1);
            return new WpResult().success(authenticationInfo);
        }catch (WpAuthenticationException e){
            return new WpResult().failure(e.getCode() + "",e.getMessage());
        }
    }

    /**
     * 退出登录
     * @return
     */
    @RequestMapping("/logoutApp")
    public WpResult logoutApp(){
        String tokenName = authenticationProperties.getTokenName();
        appLoginService.logout();
        // 移除cookie
        CookieUtil.addCookie(WpWebContext.getResponse(),tokenName,null,0);
        return new WpResult<AuthenticationInfo>().success();
    }

    /**
     * 啥都不做，就检测下登录状态
     * @return
     */
    @RequestMapping("/check")
    public WpResult check(){
        return new WpResult<AuthenticationInfo>().success();
    }



}
